<?php
/**
 * @package Salary
 * @author Oliver Leisalu
 */
class Salary {
	
	/**
	 * Neto salary
	 * @var Float
	 */
	public $neto = 0;
	/**
	 * Bruto salary
	 * @var Float
	 */
	public $bruto = 0;
	/**
	 * Wagefund - company total expenses
	 * @var Float
	 */
	public $wageFund = 0;
	
	
	/**
	 * @var Float
	 */
	public $incomeTax = 0;
	/**
	 * @var Float
	 */
	public $socialTax = 0;
	/**
	 * @var Float
	 */
	public $pension2Sammas = 0;
	/**
	 * @var Float
	 */
	public $unemploymentInsuranceEmployer = 0;
	/**
	 * @var Float
	 */
	public $unemploymentInsuranceEmployee = 0;
	
	
	/**
	 * @var Int
	 */
	public $taxFreeMinimum = 2250;
	/**
	 * @var Int
	 */
	public $pensioni2SammasPercent = 2;
	/**
	 * @var Int
	 */
	public $unemploymentInsuranceEmployerPercent = 0.5;
	/**
	 * @var Int
	 */
	public $unemploymentInsuranceEmployeePercent  = 1;
	/**
	 * @var Int
	 */
	public $incomeTaxPercent = 21;
	/**
	 * @var Int
	 */
	public $socialTaxPercent = 33;
	
	
	/**
	 * How many hours worker has to work per month
	 * @var Int
	 */
	public $workHours = 168;
	/**
	 * @var Float
	 */
	public $hourlyNeto;
	/**
	 * @var Float
	 */
	public $hourlyBruto;
	/**
	 * @var Float
	 */
	public $hourlyWageFund;
	
	/**
	 * Calculates all payments from bruto price
	 * @param Float $price
	 * @param String $type [neto|bruto|wageFund]
	 */
	public function calculate( $price, $type = 'neto') {
		
		if( $type == 'neto' ) {
			$X = ( 1 - ( 1 - $this->unemploymentInsuranceEmployeePercent/100 - $this->pensioni2SammasPercent/100) * $this->incomeTaxPercent/100 - ($this->pensioni2SammasPercent/100 + $this->unemploymentInsuranceEmployeePercent/100));
			$price = ($price - $this->taxFreeMinimum * $this->incomeTaxPercent/100) / $X;		
		} else if( $type == 'wageFund' ) {
			$price = $price / ( 1 + $this->socialTaxPercent/100 + $this->unemploymentInsuranceEmployerPercent/100);
			
		}
		
		if($price<0) $price = 0;
		
		$this->bruto = round($price,2);
		
		$this->unemploymentInsuranceEmployee = $this->bruto*$this->unemploymentInsuranceEmployeePercent/100;
		$this->pension2Sammas = $this->bruto*$this->pensioni2SammasPercent/100;
		$this->incomeTax = ($this->bruto - $this->taxFreeMinimum - $this->unemploymentInsuranceEmployee - $this->pension2Sammas)*$this->incomeTaxPercent/100;
		if( $this->incomeTax < 0 )
			$this->incomeTax = 0;
		
		$this->neto = $this->bruto - $this->incomeTax - $this->pension2Sammas - $this->unemploymentInsuranceEmployee;

		$this->unemploymentInsuranceEmployer = $this->bruto*$this->unemploymentInsuranceEmployerPercent/100;
		$this->socialTax = $this->bruto*$this->socialTaxPercent/100;
		$this->wageFund = $this->bruto + $this->socialTax + $this->unemploymentInsuranceEmployer;
		
		if( $this->workHours > 0 ) {
			$this->hourlyNeto = round($this->neto / $this->workHours, 2);
			$this->hourlyBruto = round($this->bruto / $this->workHours, 2);
			$this->hourlyWageFund = round($this->wageFund / $this->workHours, 2);
		} else {
			$this->hourlyNeto = 0;
			$this->hourlyBruto = 0;
			$this->hourlyWageFund = 0;
		}
	}
	
	
}
?>